package com.ztc.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ztc.bean.ExcelModel;
import com.ztc.bean.ProjectModel;
import com.ztc.bean.ReportModel;

/**
 * @类名：ProjectDao.java
 * @作者：李晓平
 * @时间：2018年6月4日 下午4:52:50
 * @描述：项目管理Dao
 */
@Mapper
public interface ProjectDao {
	
	/**
	 * 评估对象
	 * 
	 * @Title: queryEvaluateObjectList
	 * @Description:
	 * @return List<Map<String,String>>
	 * @date 2018年12月20日下午4:45:00
	 */
	@Select("SELECT id,name FROM tab_pre_project_evaluate_object_list")
	public List<Map<String, String>> queryEvaluateObjectList();
	
	@Select("select group_concat(realName order by userId) as realName from tab_user where userId in (${userIds}) order by userId")
	public String queryUserNamesByIds(@Param("userIds") String userIds);
	
	@Select("select tableId from tab_relation where tableName between 'ztc_00010000' and 'ztc_00012999'")
	public List<Map<String, Object>> queryZtcRelation();
	
	@Select("select * from ztc_${relation}")
	public List<Map<String, Object>> queryAllReportContent(@Param("relation") String relation);
	
	@Update("alter table ztc_${relation} add column `pureContent` mediumtext NULL AFTER `version`")
	public int alterTable(@Param("relation") String relation);
	
	@Update("update ztc_${relation} set pureContent=#{pureContent} where contentId=#{contentId}")
	public int updateReportContent(@Param("relation") String relation, @Param("contentId") int contentId, @Param("pureContent") String pureContent);
	
	@Select("<script>"
			+ "select tp.*,"
			+ "tca.name as projectType,tor.name as projectOwner,tu1.realName manager,"
			+ "group_concat(distinct tpum.userId) memberId,group_concat(distinct tu2.realName) member"
			+ " from tab_project tp left join tab_user tu1 on tp.managerId=tu1.userId"
			+ " left join tab_project_user_map tpum1 on tpum1.projectId=tp.projectId"
			+ " left join tab_project_user_map tpum on tpum.projectId=tp.projectId"
			+ " left join tab_user tu2 on tpum.userId=tu2.userId"
			+ " left join tab_category tca on tca.id=tp.projectTypeId"
			+ " left join tab_organization tor on tor.id=tp.projectOwnerId"
			+ " where 1=1"
			+ "<if test='projectId!=null'>"
			+ " and tp.projectId=#{projectId}"
			+ "</if>"
			+ " group by tp.projectId order by updateTime desc"
			+ "</script>")
	public ProjectModel queryProjectById(@Param("projectId") int projectId);
	
	@Select("select * from ztc_${relation} where projectId=#{projectId}")
	public List<ExcelModel> queryExcel(@Param("relation") String relation, @Param("projectId") int projectId);
	
	/**
	 * 查询模板
	 * @param authId
	 * @return
	 */
	@Select("select tr.template from tab_relation tr where tr.tableId=#{relation}")
	public List<ReportModel> queryTemplateByRelation(@Param("relation") String relation);
}
